import { mongo } from '#nuxt-mongodb'

export default defineEventHandler(async (event) => {
    const db = mongo.db()
    try {
        // 执行查询并转换游标为数组
        const response = await db.collection('user').find()
        // console.log(response)
        const responseArray = await response.toArray()
        // 构建正确的响应对象
        const responseObject = {
            status: 200,
            message: 'Data retrieved successfully',
            data: responseArray // 直接返回数组形式的数据
        }
        return JSON.stringify(responseObject) // 将整个响应对象转换为JSON字符串
    } catch (error:any) {
        // 错误处理，返回错误信息
        return JSON.stringify({
            status: 500,
            message: 'Failed to retrieve data',
            error: error.message
        })
    }
})